package com.xyht.sca_s.student_manage_system.modules.assetFixedReturn.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.assetFixedReturn.entity.vo.SmsAssetFixedReturnApplyAllowVO;
import com.xyht.sca_s.student_manage_system.modules.assetFixedReturn.entity.vo.SmsAssetFixedReturnApplyExportVO;
import com.xyht.sca_s.student_manage_system.modules.assetFixedReturn.entity.vo.SmsAssetFixedReturnApplyVO;
import com.xyht.sca_s.student_manage_system.modules.assetFixedReturn.service.SmsAssetFixedReturnApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.Future;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-06-01
 */
@RestController
@RequestMapping("/assetFixedReturn")
@SecretBody
public class SmsAssetFixedReturnApplyController {
    @Autowired
    JwtTokenUtil jwtTokenUtil;
    @Autowired
    SmsAssetFixedReturnApplyService smsAssetFixedReturnApplyService;
    @ApiModelProperty(value = "提交申请")
    @PostMapping("/add")
    public ResponseResult assetFixedReturnAdd(@RequestBody SmsAssetFixedReturnApplyVO applyVO,
                                              HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsAssetFixedReturnApplyService.assetFixedReturnAdd(applyVO,user_id);
    }
    @ApiModelProperty(value = "撤销申请")
    @PostMapping("/cancel")
    public ResponseResult cancelAssetFixedReturn(@RequestBody SmsAssetFixedReturnApplyVO cancelVO,
                                                 HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsAssetFixedReturnApplyService.cancelAssetFixedReturn(cancelVO,user_id);
    }
    @ApiModelProperty(value = "获取审批列表")
    @GetMapping("/list")
    public ResponseResult getAssetFixedReturnList(@RequestParam(value = "apply_type") Integer apply_type,
                                                  @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                                  @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                  HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsAssetFixedReturnApplyService.getAssetFixedReturnList(apply_type,pageSize,pageNum,user_id);
    }
    @ApiModelProperty(value = "获取审批详情")
    @GetMapping("/detail")
    public ResponseResult getAssetFixedReturnDetail(@RequestParam(value = "apply_id") String apply_id,
                                                    HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsAssetFixedReturnApplyService.getAssetFixedReturnDetail(apply_id,user_id);
    }
    @ApiModelProperty(value = "审批")
    @PostMapping("/allow")
    public ResponseResult allowAssetFixedReturn(@RequestBody SmsAssetFixedReturnApplyAllowVO allowVO,
                                                HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsAssetFixedReturnApplyService.allowAssetFixedReturn(allowVO,user_id);
    }
    @ApiModelProperty(value = "根据归还人id查询对应的归还物资")
    @GetMapping("/selectByUserId")
    public ResponseResult selectAssetFixedReturnByUserId(@RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                                         @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                         HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsAssetFixedReturnApplyService.selectAssetFixedReturnByUserId(pageSize,pageNum,user_id);
    }
    @ApiModelProperty(value = "查询所有归还申请记录")
    @GetMapping("/selectAll")
    public ResponseResult selectAll(@RequestParam(value = "apply_person",required = false) String apply_person,
                                    @RequestParam(value = "apply_department",required = false) String apply_department,
                                    @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                    @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum){
        return smsAssetFixedReturnApplyService.selectAll(apply_person,apply_department,pageSize,pageNum);
    }
    @ApiOperation("批量导出")
    @PostMapping("/batch/export")
    public ResponseResult exportFixedReturnApplication(@RequestBody SmsAssetFixedReturnApplyExportVO exportVO,
                                                HttpServletResponse response){
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = smsAssetFixedReturnApplyService.exportFixedReturnApplication(exportVO, response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }
}

